package com.tdam2011.grupo02.data;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.domain.FilterInterface;
import com.tdam2011.grupo02.domain.Llamada;
import com.tdam2011.grupo02.domain.MensajeWeb;
import com.tdam2011.grupo02.domain.SMS;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class ContactDroidSettings {

	private Context context;
	private SharedPreferences preferences;

	private String CALL_ORDER;
	private String CALL_ORDER_TYPE;
	private String CALL_FILTER;
	private String CALL_FILTER_CURRENT_DATE;

	private String SMS_ORDER;
	private String SMS_ORDER_TYPE;
	private String SMS_FILTER;
	private String SMS_FILTER_CURRENT_DATE;

	private String MSGW_ORDER;
	private String MSGW_ORDER_TYPE;
	private String MSGW_FILTER;
	private String MSGW_FILTER_CURRENT_DATE;
	private String MSGW_SEND_LOCATION;

	private String MAIL_ORDER;
	private String MAIL_ORDER_TYPE;
	private String MAIL_FILTER_CURRENT_DATE;

	public static final String ORDER_TYPE_ASC = "0";
	public static final String ORDER_TYPE_DESC = "1";

	public static final String ORDER_DATE = "0";
	public static final String ORDER_NAME = "1";

	public static final String FILTER_ALL = "0";
	public static final String FILTER_INCOMING = "1";
	public static final String FILTER_OUTGOING = "2";
	public static final String FILTER_MISSED = "3";

	public ContactDroidSettings(Context context) {
		this.context = context.getApplicationContext();
		preferences = PreferenceManager
				.getDefaultSharedPreferences(this.context);
		// call
		CALL_ORDER = this.context.getString(R.string.settingsLogCallOrder);
		CALL_ORDER_TYPE = this.context
				.getString(R.string.settingsLogCallOrderType);
		CALL_FILTER = this.context.getString(R.string.settingsLogCallFilter);
		CALL_FILTER_CURRENT_DATE = this.context.getString(R.string.settingsLogCallCDFilter);
		// sms
		SMS_ORDER = this.context.getString(R.string.settingsLogSmsOrder);
		SMS_ORDER_TYPE = this.context
				.getString(R.string.settingsLogSmsOrderType);
		SMS_FILTER = this.context.getString(R.string.settingsLogSmsFilter);
		SMS_FILTER_CURRENT_DATE = this.context.getString(R.string.settingsLogSmsCDFilter);
		// web message
		MSGW_ORDER = this.context.getString(R.string.settingsLogMsgwOrder);
		MSGW_ORDER_TYPE = this.context
				.getString(R.string.settingsLogMsgwOrderType);
		MSGW_FILTER = this.context.getString(R.string.settingsLogMsgwFilter);
		MSGW_FILTER_CURRENT_DATE = this.context.getString(R.string.settingsLogMsgwCDFilter);
		MSGW_SEND_LOCATION = this.context.getString(R.string.settingsMsgwEnviarUbicacion);
		// mails
		MAIL_ORDER = this.context.getString(R.string.settingsLogMailOrder);
		MAIL_ORDER_TYPE = this.context
				.getString(R.string.settingsLogMailOrderType);
		MAIL_FILTER_CURRENT_DATE = this.context.getString(R.string.settingsLogMailCDFilter);
	}

	public String getOrder(FilterInterface filterInterface) {
		if (filterInterface instanceof Llamada)
			return getCallOrder();
		else if (filterInterface instanceof SMS)
			return getSmsOrder();
		else if (filterInterface instanceof MensajeWeb)
			return getMsgwOrder();
		else
			return getMailOrder();
	}

	public String getOrderType(FilterInterface filterInterface) {
		if (filterInterface instanceof Llamada)
			return getCallOrderType();
		else if (filterInterface instanceof SMS)
			return getSmsOrderType();
		else if (filterInterface instanceof MensajeWeb)
			return getMsgwOrderType();
		else
			return getMailOrderType();
	}

	public String getFilter(FilterInterface filterInterface) {
		if (filterInterface instanceof Llamada)
			return getCallFilter();
		else if (filterInterface instanceof SMS)
			return getSmsFilter();
		else
			return getMsgwFilter();
	}
	
	public boolean isFilterCurrentDate(FilterInterface filterInterface) {
		if (filterInterface instanceof Llamada)
			return isCallFilterCurrentDate();
		else if (filterInterface instanceof SMS)
			return isSmsFilterCurrentDate();
		else if(filterInterface instanceof MensajeWeb)
			return isMsgwFilterCurrentDate();
		else
			return isMailFilterCurrentDate();
	}

	/*********************** Call Settings ***********************/

	public String getCallOrder() {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(this.context);
		return preferences.getString(CALL_ORDER, ORDER_DATE);
	}

	public String getCallOrderType() {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(this.context);
		return preferences.getString(CALL_ORDER_TYPE, ORDER_TYPE_DESC);
	}

	public String getCallFilter() {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(this.context);
		return preferences.getString(CALL_FILTER, FILTER_ALL);
	}
	
	public boolean isCallFilterCurrentDate() {
		 return preferences.getBoolean(CALL_FILTER_CURRENT_DATE, false);
	}

	/*********************** Sms Settings ***********************/

	public String getSmsOrder() {
		return preferences.getString(SMS_ORDER, ORDER_DATE);
	}

	public String getSmsOrderType() {
		return preferences.getString(SMS_ORDER_TYPE, ORDER_TYPE_DESC);
	}

	public String getSmsFilter() {
		return preferences.getString(SMS_FILTER, FILTER_ALL);
	}
	
	public boolean isSmsFilterCurrentDate() {
		 return preferences.getBoolean(SMS_FILTER_CURRENT_DATE, false);
	}

	/*********************** Web Messages Settings ***********************/

	public String getMsgwOrder() {
		return preferences.getString(MSGW_ORDER, ORDER_DATE);
	}

	public String getMsgwOrderType() {
		return preferences.getString(MSGW_ORDER_TYPE, ORDER_TYPE_DESC);
	}

	public String getMsgwFilter() {
		return preferences.getString(MSGW_FILTER, FILTER_ALL);
	}

	public boolean isMsgwFilterCurrentDate() {
		 return preferences.getBoolean(MSGW_FILTER_CURRENT_DATE, false);
	}
	
	public boolean isMsgwSendLocation(){
		return preferences.getBoolean(MSGW_SEND_LOCATION, false);
	}

	/*********************** Mail Settings ***********************/

	public String getMailOrder() {
		return preferences.getString(MAIL_ORDER, ORDER_DATE);
	}

	public String getMailOrderType() {
		return preferences.getString(MAIL_ORDER_TYPE, ORDER_TYPE_DESC);
	}

	public boolean isMailFilterCurrentDate() {
		 return preferences.getBoolean(MAIL_FILTER_CURRENT_DATE, false);
	}
}
